﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using plas.core.support;
using plas.cps.api.entity;
using plas.cps.api.services;
using plas.core.entity;
using plas.core.Tools;
using System.Linq.Expressions;

namespace plas.cps.impl.service
{
    public class HotelServicesImpl : AbstractBaseServiceSupport<HotelEntity>, IHotelServices
    {
        /// <summary>
        /// 保存酒店实体，并且验证数据
        /// </summary>
        /// <param name="hotels"></param>
        public void Save(params HotelEntity[] hotels)
        {
            foreach(HotelEntity hotel in hotels)
            {
                CheckHotelssModel(hotel);
                if (hotel.Operate == BaseEntity.OperateType.OPERATE_INSERT)
                {
                    hotel.HotelPid = UUIDMaker.makUUID();
                    this.dao.insert(hotel);
                    hotel.Operate = BaseEntity.OperateType.OPERATE_UPDATE;

                }
                else if (hotel.Operate == BaseEntity.OperateType.OPERATE_UPDATE)
                {
                    if (string.IsNullOrEmpty(hotel.HotelPid))
                    {
                        throw new Exception("酒店编号不可为空");
                    }
                    this.dao.update(hotel);
                }
            }
        }
        /// <summary>
        /// 删除酒店信息
        /// </summary>
        /// <param name="pk">酒店主键数组</param>
        public void Delete(params string[] pks)
        {
            foreach (string pid in pks)
            {
                if (string.IsNullOrEmpty(pid))
                {
                    throw new Exception("请选择需要删除的酒店");
                }
                this.dao.delete(pid);
            }
        }
        /// <summary>
        /// 通过id查询
        /// </summary>
        /// <param name="pk">酒店资源主键</param>
        public HotelEntity QueryHotelById(string pk)
        {
            if (string.IsNullOrEmpty(pk))
            {
                throw new Exception("请提供查询物品的id");
            }
            return this.dao.queryById(pk);
        }
        /// <summary>
        /// 多条件查询
        /// </summary>
        /// <param name="pageNum">当前页</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="hotel">酒店资源实体</param>
        /// <param name="orderBy">键值对, 可以不赋值，参见实例 </param>
        /// <returns></returns>
        public core.entity.QueryResult<HotelEntity> QueryList(int pageNum, int pageSize, HotelEntity hotels, Dictionary<string, string> orderBy)
        {
            if (hotels != null)
            {
                Expression<Func<HotelEntity, bool>> body = PredicateExtensionsUtil.True<HotelEntity>();
                if (!string.IsNullOrEmpty(hotels.HotelName))//通过物品名称查询
                    body = body.And(o => o.HotelName.Contains(hotels.HotelName));
                return this.dao.queryList(pageNum, pageSize, null, orderBy);
            }
            else
            {
                return this.dao.queryList(pageNum, pageSize, null, orderBy);
            }
        }
        private void CheckHotelssModel(HotelEntity hotel)
        {
            if (hotel == null)
            {
                throw new Exception("酒店信息不可为空");
            }
            if (string.IsNullOrEmpty(hotel.HotelName))
            {
                throw new Exception("酒店名称不可为空");
            }
            if (string.IsNullOrEmpty(hotel.HotelAddress))
            {
                throw new Exception("酒店地址不可为空");
            }
            if (double.IsNaN(hotel.HotelTel))
            {
                throw new Exception("酒店电话不可为空");
            }
            if (string.IsNullOrEmpty(hotel.HotelRname))
            {
                throw new Exception("酒店联系人不可为空");
            }
            if (String.IsNullOrEmpty((hotel.HotelMaxPeople).ToString()))
            {
                throw new Exception("最大容客量不可为空");
            }
            if (String.IsNullOrEmpty((hotel.HotelMax).ToString()))
            {
                throw new Exception("可承办婚礼数量不可为空");
            }
        }
    }
}
